package yl.hs.bmipfull.controllers.material;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.material.IRawMaterialCateService;
import yl.hs.bmipfull.viewmodels.OptionModel;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.ArrayList;
import java.util.List;

/**
 * 作用:  原材分类信息控制层<br/>
 * 创建时间: 2021年05月17日 08时49分39秒 <br/>
 *
 * @author 张凯
 */
@Api(value = "原材分类信息Web接口", tags = "采购管理——原材分类信息操作")
@RequestMapping(value = "/api/rawMaterial")
@RestController
public class RawMaterialCateController extends ControllerBase {

    @Autowired
    IRawMaterialCateService service;

    @ApiOperation(value = "查询原材分类信息", notes = "查询原材分类信息")
    @RequestMapping(value = "/cate/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询原材分类信息", notes = "查询原材分类信息")
    @RequestMapping(value = "/cate", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询原材分类信息", notes = "查询原材分类信息get")
    @RequestMapping(value = {"/allCate/{productSN}", "/cate"}, method = RequestMethod.GET)
    public WebResult findList(@PathVariable(required = false, name = "productSN") String productSN) {
        try {
            List<OptionModel> model = new ArrayList<>();
            var result = service.findList(productSN);
            result.forEach(t -> {
                model.add(new OptionModel(t.getSn(), t.getCateName(), null));
            });
            return success("查询成功", model);
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询原材分类信息", notes = "根据原材分类信息编号查询原材分类信息")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }


    @ApiOperation(value = "保存原材分类信息", notes = "保存/更新原材分类信息信息")
    @RequestMapping(value = "/cate", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveRawMaterialCate(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else if (result == -1) {
                return fail("名称重复",false);
            } else{
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "根据原材种类名称查询原材种类", notes = "根据原材种类名称查询原材种类")
    @RequestMapping(value = "/chackName", method = RequestMethod.GET)
    public WebResult chackName(@ApiParam(value = "原材种类名称") @RequestParam(name = "cateName") String cateName,@ApiParam(value = "原材种类编号") @RequestParam(name = "sn",required = false) String sn) {
        boolean result=false;
        if(sn==null || sn.equals("null") || sn==""){
            result=service.queryByName(cateName,null);
        }else{
            result=service.queryByName(cateName,sn );
        }
        return success("ok", result);
    }

    @ApiOperation(value = "删除原材分类信息信息", notes = "根据原材分类信息编号删除系原材分类信息信息")
    @RequestMapping(path = "/cate/{sn}", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "分类编号", name = "sn") @PathVariable String sn) {
        if (service.delete(sn.split(",")) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }

    @ApiOperation(value = "根据编号修改状态", notes = "根据编号修改状态")
    @RequestMapping(value = "/cate/changeStateBySn/{sn}/{state}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult changeStateBySn(@PathVariable String sn, @PathVariable short state) {
        try {
            Integer result = service.changeStateBySn(sn.split(","), state);
            if (result > 0) {
                return success("数据已修改");
            } else if (result == -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }
}